package com.example.demo.controller;

import com.example.demo.entity.Customer;
import com.example.demo.entity.Result;
import com.example.demo.service.ResultService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/result")
public class ResultController {
    @Resource
    private ResultService resultService;

    /**
     * 写入一条新的中奖纪录
     * @param result
     * @return
     */
    @RequestMapping("/addNewResult")
    @ResponseBody
    public Integer addNewResult(Result result){
        Integer rel=0;
        rel = resultService.addNewResult(result);
        return rel;
    }

    /**
     * 通过客户编号获取一条中奖记录
     * @param userId
     * @return
     */
    @RequestMapping("/getResultByUserId")
    @ResponseBody
    public Result getResultByUserId(Integer userId){
        int rel=0;
        Result result = resultService.getResultByUserId(userId);
        if(result!=null){
            rel=1;
        }
        return result;
    }

    /**
     * 第二次登陆抽奖系统时通过客户编号获取一条中奖记录
     * @return
     */
    @RequestMapping("/twiceGetResultByUserId")
    @ResponseBody
    public Map twiceGetResultByUserId(HttpSession session){
        Map map = new HashMap();
        Customer customer =(Customer) (session.getAttribute("customer"));
        Result result = resultService.getResultByUserId(customer.getUserId());
        map.put("result",result);
        map.put("customer",customer);
        return map;
    }
}
